#include <allegro.h>
#include <pthread.h>

#include "projet.h"

void clikclik(int a)
{
    pthread_t thr[7];
    void *ret;
    int x = mouse_x;
    int y = mouse_y;
    int i;
    int nb[7]={0,1,2,3,4,5,6};
    carte[x/10][y/10].evenement = a;
    for(i=0;i<7;i++)
        pthread_create(&(thr[i]),NULL, dijkstra_calc,&(nb[i]));
    for(i=0;i<7;i++)
        pthread_join(thr[i],&ret);
}  

void bordel()
{
    int x,y;
    for(x=0;x<X_CARTE;x++)
    {
        for(y=0;y<Y_CARTE;y++)
        {
            carte[x][y].evenement = rand()%3;
        }
    }
}
 
void dijkstra_display2(int aeroport)
{
    int x,y;
    int max=0;
    for(x=0;x<X_CARTE;x++)
    {
        for(y=0;y<Y_CARTE;y++)
        {
            if(max<dijkstra[aeroport][x][y].dp) max = dijkstra[aeroport][x][y].dp;
        }
    }
    for(x=0;x<X_CARTE;x++)
    {
        for(y=0;y<Y_CARTE;y++)
        {
            rectfill(screen,10*x,10*y,10*(x+1),10*(y+1),makecol(dijkstra[aeroport][x][y].dp*255/max,255-dijkstra[aeroport][x][y].dp*255/max,0));
        }
    }
    if(aeroport!=0)
    {
    for(x=0;x<X_CARTE;x++)
    {
        for(y=0;y<Y_CARTE;y++)
        {
            line(screen, 5+10*x, 5+10*y,
                         5+10*dijkstra[aeroport][x][y].xprec,5+10*dijkstra[aeroport][x][y].yprec,
                         makecol(0,0,0));
        }
    }}
}
 
int main(int nArgs,char* Args[])
{
	int touche,i,x,y;
	char txt[64];
	int aeroport=0;
	t_aeroport *aero=NULL;
	
    
    init_simulation();
    chargement();
    
    //bordel();
    
    DIJKSTRA_MODE = EQUI;
    
    i=0;
	for(y=0;y<Y_CARTE;y++)
	{
		for(x=0;x<X_CARTE;x++)
		{
			if(carte[x][y].aeroport!=NULL)
			{
				if(0 == i) aero = carte[x][y].aeroport;
				i++;
			}
		}
	}
  
	install_allegro(SYSTEM_AUTODETECT, &errno, atexit);
	install_keyboard();
	install_mouse();
	set_color_depth(24);
	set_gfx_mode(GFX_AUTODETECT, 600, 300, 0, 0);
	show_mouse(screen);
	do
	{
		//clear_bitmap(screen);
		sleep(50);
		if(mouse_b & 1) {clikclik(1); dijkstra_display2(aeroport);}
		if(mouse_b & 2) {clikclik(0); dijkstra_display2(aeroport);}
		if(keypressed())
		{
			touche = readkey()>>8;
			if(touche == KEY_Q) aeroport = 0;
			if(touche == KEY_W) aeroport = 1;
			if(touche == KEY_E) aeroport = 2;
			if(touche == KEY_R) aeroport = 3;
			if(touche == KEY_T) aeroport = 4;
			if(touche == KEY_Y) aeroport = 5;
			if(touche == KEY_U) aeroport = 6;
			i=0;
	        for(y=0;y<Y_CARTE;y++)
	        {
                for(x=0;x<X_CARTE;x++)
                {
			        if(carte[x][y].aeroport!=NULL)
			        {
				        if(aeroport == i) aero = carte[x][y].aeroport;
				        i++;
		            }
                }
            }
            dijkstra_display2(aeroport);
            textprintf_ex(screen,font,0,0,makecol(255,0,0),-1,"aeroport : %s",aero->nom);
		}
		clear_keybuf();
	} while(touche != KEY_N);
	return 0;
}
END_OF_MAIN();
